﻿using Domain.Flow;
using MediatR;
using U.B.ErrorCode;
using U.B.ReadApi.Command.Raddit.BW;
using U.B.ReadApi.Dto;
using U.B.Repistory;

namespace U.B.ReadApi.CommandHander.Raddit.BWHA
{
    public class GetrabbitCommandHander : IRequestHandler<GetrabbitCommand, Apipage<ProductDto>>
    {
        public readonly IRepistory<Product> _Productrepistory;
        public readonly IRepistory<ProductClass>_ProductClassrepistory;
        public GetrabbitCommandHander(IRepistory<Product> productrepistory, IRepistory<ProductClass> productClassrepistory)
        {
            _Productrepistory = productrepistory;
            _ProductClassrepistory = productClassrepistory;
        }
        /// <summary>
        /// 商品显示列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<Apipage<ProductDto>> Handle(GetrabbitCommand request, CancellationToken cancellationToken)
        {
            Apipage <ProductDto> apipage = new Apipage<ProductDto>();
            var result = from a in _Productrepistory.GetAll()
                         join b in _ProductClassrepistory.GetAll() on a.ProductClassId equals b.ProductClassId
                         select new ProductDto
                         {
                             ProductName = b.ProductName,
                             BatchId = a.BatchId,
                             FirmName = a.FirmName,
                             Full = a.Full,
                             KeepSafe = a.KeepSafe,
                             Permission = a.Permission,
                             ProductId = a.ProductId,
                             CreationDate = a.CreationDate,
                             Creator = a.Creator,

                         };
            apipage.code = Renum.显示成功;
            apipage.Message=Renum.显示成功.ToString();
            apipage.TotalCount=result.Count();
            apipage.PageCount = (int)Math.Ceiling(apipage.TotalCount * 1.0 / request.PageSize);
            apipage.Items=result.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return Task.FromResult(apipage);

        }
    }
}
